
var optType = 1, optIndex = 0, optRow = {},
    optDictIndex = 0, optDictRow = {};
var dict = {
    module: {
        title: '',
        height: kit.wh().height - 40,
        singleSelect: true,
        pagination: false,
        onClickRow:function () {
            var rows = component.datagrid.getRowData('#table-module');
            $('#panel-dict').panel({title: '[' +rows.name + '/'+ rows.code+'] 字典数据'})
            action.getDict(rows.id);
        },
        columns:[
            [
                {field:'name',title:'模块名称',align:'center'},
                {field:'code',title:'模块编码',align:'center'},
                {field:'sort',title:'排序',align:'center'}
            ]
        ]
    },
    base: {
        title: '',
        height: kit.wh().height - 40,
        singleSelect: true,
        pagination: false,
        onClickRow:function () {
            var rows = component.datagrid.getRowData('#table-module');

        },
        columns:[
            [
                {field:'name',title:'字典名称',align:'center'},
                {field:'code',title:'字典编码',align:'center'},
                {field:'sort',title:'排序',align:'center'}
            ]
        ]
    },
    form:{
        title: '字典操作',
        width:400,
        height:250,
        buttons: [{
            text:'保 存',
            iconCls:'icon-accept icon16',
            handler:function(){
                switch (optType){
                    case 1:
                        action.add('#table-module');
                        break;
                    case 2:
                        action.edit(optRow.id, optIndex, '#table-module');
                        break;
                    case 3:
                        action.add('#table-dict');
                        break;
                    case 4:
                        action.edit(optDictRow.id, optDictIndex, '#table-dict');
                        break;
                }
            }
        },{
            iconCls:'icon-cancel icon16',
            text:'关 闭',
            handler:function(){
                component.dialog.close('#opt-form');
            }
        }]
    }
}

$(function () {
    component.datagrid.init('#table-dict', dict.base, false);
    action.getModule();
})

var opt = {
    module:{
        add:function (_event) {
            dict.form.title = '创建模块';
            dict.form.height = 250;
            component.dialog.init('#opt-form', dict.form);
            component.dialog.open('#opt-form');
            kit.form.clean('#opt-form-data');
            optType = 1;
            $('#dict-val').hide();
        },
        edit:function (_event) {
            var rows = component.datagrid.getRowData('#table-module');
            var index = component.datagrid.getDataRow('#table-module');
            if(rows == null){
                kit.alert('请先选择一个字典模块', 'warning');
                return false;
            }
            dict.form.height = 250;
            component.dialog.init('#opt-form', dict.form);
            component.dialog.open('#opt-form');
            kit.form.set('#opt-form-data', rows);
            optType = 2;
            optIndex = index;
            optRow = rows;
            $('#dict-val').hide();
        },
        delete:function (_event) {
            var rows = component.datagrid.getRowData('#table-module');
            var index = component.datagrid.getDataRow('#table-module');
            if(rows == null){
                kit.alert('请先选择一个字典模块', 'warning');
                return false;
            }
            var fun = "action.delete('"+rows.id+"', "+index+");";
            var c = {title: '删除模块', message: '确定要删除这个字典模块', submit: fun }
            kit.confirm(c);
        }
    },
    dict:{
        add:function (_event) {
            var rows = component.datagrid.getRowData('#table-module');
            if(rows == null){
                kit.alert('请先选择一个模块', 'warning');
                return false;
            }
            dict.form.title = '创建字典';
            component.dialog.init('#opt-form', dict.form);
            component.dialog.open('#opt-form');
            kit.form.clean('#opt-form-data');
            kit.form.set('#opt-form-data', {moduleId: rows.id, parentId: rows.id});
            optType = 3;
        },
        edit:function (_event) {
            var rows = component.datagrid.getRowData('#table-dict');
            var index = component.datagrid.getDataRow('#table-dict');
            if(rows == null){
                kit.alert('请先选择一个字典', 'warning');
                return false;
            }
            component.dialog.init('#opt-form', dict.form);
            component.dialog.open('#opt-form');
            kit.form.set('#opt-form-data', rows);
            optType = 4;
            optDictIndex = index;
            optDictRow = rows;
        },
        delete:function (_event) {
            var rows = component.datagrid.getRowData('#table-dict');
            var index = component.datagrid.getDataRow('#table-dict');
            if(rows == null){
                kit.alert('请先选择一个字典', 'warning');
                return false;
            }
            optType = 3;
            var fun = "action.delete('"+rows.id+"', "+index+");";
            var c = {title: '删除字典', message: '确定要删除这个字典数据', submit: fun }
            kit.confirm(c);
        }
    }
}
var action = {
    getModule:function () {
        var param = {
            url: constant.route.dict.module
        }
        var request = http.get(param);
        request.done(function (d) {
            var data = d.result;
            dict.module['data'] = data;
            component.datagrid.init('#table-module', dict.module, false);
        })
    },
    getDict:function (_moduleId) {
        var param = {
            url: constant.route.dict.base + '/' + _moduleId
        }
        var request = http.get(param);
        request.done(function (d) {
            var data = d.result;
            component.datagrid.load('#table-dict', data);
        })
    },
    add:function (_table) {
        var param = {
            url: optType == 3 ? constant.route.dict.base : constant.route.dict.module,
            data: kit.form.get('#opt-form-data')
        }
        var request = http.post(param);
        request.done(function (d) {
            var data = d.result;
            kit.form.clean('#opt-form-data');
            if(optType == 3){
                kit.form.set('#opt-form-data', {moduleId: data.moduleId, parentId: data.parentId});
            }
            kit.alert(d.message, 'success');
            component.datagrid.append(_table, data);
        })
    },
    edit:function (_id, _index, _table) {
        var form = kit.form.get('#opt-form-data');
        form['id'] = _id;
        var param = {
            url: optType == 3 ? constant.route.dict.base : constant.route.dict.module,
            data: form
        }
        var request = http.put(param);
        request.done(function (d) {
            var data = d.result;
            kit.alert(d.message, 'success');
            component.datagrid.updateRow(_table, _index, data);
            component.dialog.close('#opt-form');
        })
    },
    delete:function (_id, _index) {
        var param = {
            url: optType == 3 ? constant.route.dict.base + '/' + _id : constant.route.dict.module + '/' + _id
        }
        var request = http.delete(param);
        request.done(function (d) {
            kit.alert(d.message, 'success');
            if(optType == 3){
                component.datagrid.deleteRow('#table-dict', _index);
            }else {
                component.datagrid.deleteRow('#table-module', _index);
            }
        })
    },
}
